package com.neuedu.system.service.impl;
import lombok.RequiredArgsConstructor;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neuedu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neuedu.system.domain.YxynJiatingchengyuan;
import com.neuedu.system.mapper.YxynHuzuMapper;
import com.neuedu.system.domain.YxynHuzu;
import com.neuedu.system.service.IYxynHuzuService;

/**
 * 户主信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class YxynHuzuServiceImpl extends ServiceImpl<YxynHuzuMapper, YxynHuzu> implements IYxynHuzuService
{
    private final YxynHuzuMapper yxynHuzuMapper;

    /**
     * 查询户主信息
     *
     * @param id 户主信息主键
     * @return 户主信息
     */
    @Override
    public YxynHuzu selectYxynHuzuById(Long id)
    {
        return yxynHuzuMapper.selectYxynHuzuById(id);
    }

    /**
     * 查询户主信息列表
     *
     * @param yxynHuzu 户主信息
     * @return 户主信息
     */
    @Override
    public List<YxynHuzu> selectYxynHuzuList(YxynHuzu yxynHuzu)
    {
        return yxynHuzuMapper.selectYxynHuzuList(yxynHuzu);
    }

    /**
     * 新增户主信息
     *
     * @param yxynHuzu 户主信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYxynHuzu(YxynHuzu yxynHuzu)
    {
        int rows = yxynHuzuMapper.insertYxynHuzu(yxynHuzu);
        insertYxynJiatingchengyuan(yxynHuzu);
        return rows;
    }

    /**
     * 修改户主信息
     *
     * @param yxynHuzu 户主信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYxynHuzu(YxynHuzu yxynHuzu)
    {
        yxynHuzuMapper.deleteYxynJiatingchengyuanByHouseholdId(yxynHuzu.getId());
        insertYxynJiatingchengyuan(yxynHuzu);
        return yxynHuzuMapper.updateYxynHuzu(yxynHuzu);
    }

    /**
     * 批量删除户主信息
     *
     * @param ids 需要删除的户主信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYxynHuzuByIds(Long[] ids)
    {
        yxynHuzuMapper.deleteYxynJiatingchengyuanByHouseholdIds(ids);
        return yxynHuzuMapper.deleteYxynHuzuByIds(ids);
    }

    /**
     * 删除户主信息信息
     *
     * @param id 户主信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYxynHuzuById(Long id)
    {
        yxynHuzuMapper.deleteYxynJiatingchengyuanByHouseholdId(id);
        return yxynHuzuMapper.deleteYxynHuzuById(id);
    }

    /**
     * 新增家庭成员信息信息
     *
     * @param yxynHuzu 户主信息对象
     */
    public void insertYxynJiatingchengyuan(YxynHuzu yxynHuzu)
    {
        List<YxynJiatingchengyuan> yxynJiatingchengyuanList = yxynHuzu.getYxynJiatingchengyuanList();
        Long id = yxynHuzu.getId();
        if (StringUtils.isNotNull(yxynJiatingchengyuanList))
        {
            List<YxynJiatingchengyuan> list = new ArrayList<YxynJiatingchengyuan>();
            for (YxynJiatingchengyuan yxynJiatingchengyuan : yxynJiatingchengyuanList)
            {
                yxynJiatingchengyuan.setHouseholdId(id);
                list.add(yxynJiatingchengyuan);
            }
            if (list.size() > 0)
            {
                yxynHuzuMapper.batchYxynJiatingchengyuan(list);
            }
        }
    }
}
